package com.yan.springbootstudy.common.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.DispatcherServlet;

import com.yan.springbootstudy.common.datasource.DataOperationContext;
import com.yan.springbootstudy.common.datasource.DataOperationContextHolder;

/**
 * 
 * @Title: DataOperationServlet.java
 * @Package com.yan.springbootstudy.common.servlet
 * @Description: 直接继承修改Spring的DispatcherServlet，写操作关键变量到线程，可以少定义一个filter（测试未通过）
 * @author yan
 * @date 2017年8月30日
 * @version V1.0
 */
public class DataOperationServlet extends DispatcherServlet {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		logger.debug("servletChangeDataOperation");
		DataOperationContext dataOperationContext = new DataOperationContext();
		//得到请求方法类型
		dataOperationContext.setOperation(request.getMethod());
		logger.debug("operation:" + dataOperationContext.getOperation());
		//得到关键业务属性
		//得到用户id
		dataOperationContext.setUserId(request.getParameter("userId"));
		logger.debug("userId:" + dataOperationContext.getUserId());
		DataOperationContextHolder.setDataOperationContext(dataOperationContext);
		super.service(request, response);
    }

}
